package cn.bellychang.bobo.queue;

import cn.bellychang.bobo.arraylist.Iterator;
import cn.bellychang.bobo.linkedlist.TwoWayLinkedList;

/**
 * @author ChangLiang
 * @date 2020/9/11
 */
public class TwoWayLinkedListQueue<E> implements Queue<E> {

    private TwoWayLinkedList<E> list;

    public TwoWayLinkedListQueue() {
        list = new TwoWayLinkedList<>();
    }

    @Override
    public int getSize() {
        return list.getSize();
    }

    @Override
    public boolean isEmpty() {
        return list.getSize() == 0;
    }

    @Override
    public void enqueue(E e) {
        list.addLast(e);
    }

    @Override
    public E dequeue() {
        return list.removeFirst();
    }

    @Override
    public E getFront() {
        return list.getFirst();
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Queue Head {");
        Iterator<E> iterator = list.iterator();
        while (iterator.hasNext()) {
            builder.append(iterator.next());
            if (iterator.hasNext()) {
                builder.append("<-");
            }
        }
        return builder.append("} Tail").toString();
    }
}
